Skip to content

Conversation

@firewave
Copy link
Collaborator

No description provided.

@sonarqubecloud
Copy link

@firewave
Copy link
Collaborator Author

firewave commented Oct 17, 2025

If this is acceptable we probably can use this pattern for other classes which currently require friend declarations to be tested.

Comment on lines +94 to +96
class CmdLineParserTest : public CmdLineParser
{
friend class TestCmdlineParser;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This allows us to access the protected members.

Copy link
Owner

@danmar danmar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see no nice solution.

  • Friend is horrible but at least it explicitly pointed out the test class.
  • The disadvantage with this solution is that if we would ever like to use CmdLineParser as a base class in production code then the derived classes have access to stuff that should really be private.

I have no strong opinion. But I fear that sooner or later somebody will come and look at the code and in his opinion we should have solved it in another way and then we get another PR..

@firewave
Copy link
Collaborator Author

  • Friend is horrible but at least it explicitly pointed out the test class.

I think it is perfectly reasonable in tests. It might even be the proper way to test internals.

  • The disadvantage with this solution is that if we would ever like to use CmdLineParser as a base class in production code then the derived classes have access to stuff that should really be private.

Which you could always circumvent by using a friend declaration anyways. If you implement it properly you use the proper access specifier in the inheritance.

Also this allowed us to remove several unnecessary public accesses so you can actually access less things then before from outside the class. In some other cases I already did we can even get rid of the friend stuff all together.

Using the friend declaration in the production code also opted it out of the unusedFunction check in the CI.

@firewave
Copy link
Collaborator Author

In case of #7918 that intermediate class will go away with #7920.

It might be possible that in other cases we also adjust the interface (or the test) so it adheres to the public interface and doesn't require internals to be spilled to be tested.

@firewave firewave merged commit 3547499 into danmar:main Oct 28, 2025
54 checks passed
@firewave firewave deleted the testcmd-x branch October 28, 2025 19:29
@firewave
Copy link
Collaborator Author

I could have kept the access functions and moved them to the test classes. Something to consider for future changes like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants